<?php 
namespace town\model;

use town\dmod\DB;
use town\dmod\Model;
use town\tool\Page;

class Post extends Model
{
	public $tableName = 'posts';

	/**
	 * 获取博客列表
	 */
	public function posts($where = [], $page = 1, $pageSize = 10, $order = '')
	{
		$fields= 'id,title,slug,author,cate,views,comments,create_at,update_at';
		$page  = max($page, 1);
		$limit = (($page - 1) * $pageSize) . ',' . $pageSize;
		$datas = $this->link->where($where)->limit($limit)->lists($fields);
		foreach ($datas as $key => &$value) {
			$value['create_at'] = date('Y/m/d', $value['create_at']);
			$value['update_at'] = date('Y/m/d', $value['update_at']);
		}
		return $datas;
	}


	/**
	 * 获取详情
	 */
	public function postById($id)
	{
		$info = $this->find($id);

		// time
		$info['create_at'] = date('Y/m/d', $info['create_at']);
		$info['update_at'] = date('Y/m/d', $info['update_at']);

		// tags
		$tags = (new Tags)->tagByPostId($id);
		$info['tags'] = $tags;

		// cate
		$cate = (new Category)->cateByPostId($id);

		// TODO::author
		$author = 'peim';

		// content_html
		$html = DB::table('posts_html')->where('id', $id)->first();
		$info['content_html'] = $html['html'];

		// 处理信息
		return $info;
	}


	/**
	 * 根据 tags 获取详情列表
	 */
	public function postsByTagId($tid)
	{
		$tid  = intval($tid);

		$count= DB::table('tags_map_posts')->where('tid', $tid)->count();


		$pObj = new Page($count, 10, 0, ['tags'=>$tid]);
		$pids = DB::table('tags_map_posts')->where('tid', $tid)->orderby('id', 'DESC')
			->limit($pObj->startRow.','.$pObj->takeNum)->get();

		$ids  = '';
		foreach ($pids as $key => $value) {
			$ids .= ','.$value['pid'];
		}
		$ids  = ltrim($ids, ',');
		if (empty($ids)) {
			return [];
		}

		$fields = 'p.id,p.title,p.slug,p.author,p.cate,p.views,p.create_at,h.html';
		$sql  = "select {$fields} from posts as p inner join posts_html as h ON p.id = h.id "
			."where p.id in ({$ids}) order by p.id DESC";
		$data = DB::queryRaw($sql, function(&$row) use ($cate) {
			// create_at
			$row['create_at'] = date('Y/m/d', $row['create_at']);

			// html
			$row['html'] 	= mb_substr(strip_tags($row['html']), 0, 300, 'utf-8') . ' .....';

			// cate
			$row['cate_c']  = (new Category)->cateByPostId($row['id']);

			// tags
			$row['tags_a']  = (new Tags)->tagByPostId($row['id']);
		});

		return ['list' => $data, 'pagination'=>$pObj->html()];
	}


	/**
	 * 根据分类获取列表
	 */
	public function postsByCateId($cid)
	{
		$cid   = intval($cid);
		if ($cid < 1) {
			return ['list' => '', 'pagination'=>''];
		}
		$count = DB::table('posts')->where('cate', $cid)->count();
		$pObj  = new Page($count, 10, 0, ['cate'=>$cid]);
		$limit = $pObj->startRow.','.$pObj->takeNum;

		$fields = 'p.id,p.title,p.slug,p.author,p.cate,p.views,p.create_at,h.html';
		$sql  	= "select {$fields} from posts as p inner join posts_html as h ON p.id = h.id "
			."where p.cate={$cid} order by p.id DESC limit {$limit}";

		$data = DB::queryRaw($sql, function(&$row) use ($cate) {

			// create_at
			$row['create_at'] = date('Y/m/d', $row['create_at']);

			// html
			$row['html'] 	= mb_substr(strip_tags($row['html']), 0, 300, 'utf-8') . ' .....';

			// cate
			$row['cate_c']  = (new Category)->cateByPostId($row['id']);

			// tags
			$row['tags_a']  = (new Tags)->tagByPostId($row['id']);
		});

		return ['list' => $data, 'pagination'=>$pObj->html()];
	}


	/**
	 * 关键字获取详情
	 */
	public function postsByKeys($keys)
	{
		# code...
	}
}

